// 所有场景的基类

import * as THREE from 'three';
export default class WebGLScene {
    constructor() {
        this.models = [];
        this.store = null;
        this.loadingPercent = 0;
        this.load = false;
        this.currentZoom = 1;
        this.needTransform = false;
        this.env = 'production';
        this.Scene = new THREE.Scene();
    }

    /**
     * @function
     * @param {*} renderer // 3d渲染
     * @param {*} labelRenderer // 标签渲染
     */
    Init(renderer, application) {
        this.renderer = renderer;
        this.Scene.background = new THREE.Color('#000');
        this.clock = new THREE.Clock();
        const size = new THREE.Vector2();
        renderer.getSize(size);
        this.canvasWidth = size.width;
        this.canvasHeight = size.height;
        this.camera = new THREE.PerspectiveCamera(45, this.canvasWidth / this.canvasHeight, 0.01, 20000);
        this.Create();
    }

    Create() {
    }

    Update() {
        if (this.renderer) {
            this.renderer.render(this.Scene, this.camera);
        }
    }
    setComposser(effectComposser) {
		this.effectComposser = effectComposser;
	}

    destroyChild(obj) {
        if (typeof obj.dispose === 'function') {
            obj.dispose();
        }
        if (obj.geometry) {
            obj.geometry.dispose();
        }
        if (obj.material) {
            if (Array.isArray(obj.material)) {
                obj.material.map(m => {
                    m.dispose();
                });
            } else {
                obj.material.dispose();
            }
        }
        if (obj.children && obj.children.length) {
            obj.children.map(c => {
                this.destroyChild(c);
            });
        }
    }

    Destroy() {}

    resize() {
        const size = new THREE.Vector2();
        this.renderer.getSize(size);
        this.canvasWidth = size.width;
        this.canvasHeight = size.height;
        this.camera.aspect = this.canvasWidth / this.canvasHeight;

        this.camera.updateProjectionMatrix();
        if (this.renderer) {
            this.renderer.render(this.Scene, this.camera);
        }
    }

    /**
     * 定义挂载事件
     * @memberof EquipmentRoomScene
     * @instance
     * @param {*} eventType 事件类型
     * @param {*} callback 回调函数
     */
    on(eventType, callback) {
        if (!callback) {
            console.error("Failed to execute 'on' on 'EventTarget': 2 arguments required, but only 1 present.");
            return this;
        }
        if (typeof callback !== 'function') {
            console.error("Failed to execute 'on' on 'EventTarget': parameter 2 is not of type 'function'.");
            return this;
        }
        if (!this[`$${eventType}`]) {
            this[`$${eventType}`] = [];
        }
        if (this[`$${eventType}`].indexOf(callback) >= 0) {
            this.removeEventListener(eventType, callback);
        }
        this[`$${eventType}`].push(callback);
        return this;
    }

    /**
     * 定义移除事件的函数
     * @memberof EquipmentRoomScene
     * @instance
     * @param {*} eventType 事件类型
     * @param {*} callback 回调函数
     */
    off(eventType, callback) {
        if (!callback) {
            console.error("Failed to execute 'off' on 'EventTarget': 2 arguments required, but only 1 present.");
            return this;
        }
        if (typeof callback !== 'function') {
            console.error("Failed to execute 'off' on 'EventTarget': parameter 2 is not of type 'function'");
            return this;
        }
        if (this[`$${eventType}`] && this[`$${eventType}`].length) {
            const index = this[`$${eventType}`].indexOf(callback);
            if (index >= 0) {
                this[`$${eventType}`].splice(index, 1);
            }
        }
        return this;
    }

    /**
     * canvas通用事件的执行
     * @memberof EquipmentRoomScene
     * @instance
     * @param {Device} targetCanvasElement canvas事件目标元素 可能是设备, 可能是 线条
     * @param {Object} canvasEvent
     */
    generalEventExecute(canvasEvent) {
        if (this[`$${canvasEvent.eventType}`] && this[`$${canvasEvent.eventType}`].length) {
            this[`$${canvasEvent.eventType}`].forEach((callback) => {
                Promise.resolve().then(() => {
                    callback(canvasEvent);
                }); // 防止阻塞进程
            });
        }
    }

    zoomCamera(flag) {
        if (flag === 'add') {
            this.currentZoom *= 0.95;
        } else if (flag === 'reduce') {
            this.currentZoom /= 0.95;
        }
        this.cameraManager.zoom(this.currentZoom);
    }
}
